package com.hitqz.robot.biz.flow.component.agv;

import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/6/15
 */
@Slf4j
@LiteflowComponent(value = "LiftUpJackStatus", name = "[轮对/轮轴]顶升-上升")
@AllArgsConstructor
public class LiftUpJackStatus extends NodeComponent {
    private final RobotDriverService robotDriverService;


    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        //IRobotPosDto iRobotPosDto =robotDriverService.getWebPos();
        //int times =10;
        //for (int i=0;i < 10; i++) {
        //    //RobotIOStatusDto statusF = iRobotPosDto.getDI().stream().filter(
        //    //        t-> t.getId() == 1).findFirst().get();
        //    //RobotIOStatusDto statusB = iRobotPosDto.getDI().stream().filter(
        //    //        t-> t.getId() == 2).findFirst().get();
        //    //if (statusB.isStatus() && statusF.isStatus()) {
        //    //    log.info("前后都检测到数据，进行顶升");
        //    //    times = 0;
        //    //    break;
        //    //}
        //    RobotIOStatusDto statusS = iRobotPosDto.getDI().stream().filter(
        //            t-> t.getId()==0).findFirst().get();
        //    log.info("单独轮对的状态:" + statusS.isStatus());
        //    if (statusS.isStatus()) {
        //        log.info("单轮对检测到有轮对进行顶升");
        //        times = 0;
        //        break;
        //    }
        //    TimeUnit.SECONDS.sleep(1);
        //    iRobotPosDto =robotDriverService.getWebPos();
        //}

        //if (times > 0) {
        //    log.info("检测不到顶升货物，任务失败");
        //    LiteFlowUtil.setInterruptFlag();
        //    LiteFlo wUtil.checkInterruptFlag();
        //    return;
        //}
        robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_UP,"shelf/s0002.shelf");
        TimeUnit.SECONDS.sleep(8);
        R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
        int  jackStatus = (int) result.getData();
        int time = 0;
        while (1 != jackStatus) {
            result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
            jackStatus = (int) result.getData();
            //if (time > 50) {
            //    robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_UP,null);
            //    time = 0;
            //}
            //log.info("jackStatus" + jackStatus);
            LiteFlowUtil.checkInterruptFlag();
            time++;
        }
        log.info("lifter up finished");
    }
}
